﻿using ClassLibrary;
using Forms;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MainForm
{
    public partial class Supermarket : UIForm
    {
        ItemService itemService = new ItemService();
        User user = new User();
        public List<Good> goods = new List<Good>();//记录购买的商品
        public Supermarket()
        {
            Color userColor = MainForm.userColor;
            UIStyles.InitColorful(userColor, Color.White);
        }
        public Supermarket(User user)
        {
            InitializeComponent();
            this.user = user;
            MyOwnSupermarket.Click += MyOwnSupermarket_Click;
            MyOrder.Click += MyOrder_Click;

            List<Item> items = itemService.GetItemExceptUser(user);
            ListItems(items);//初始化商品列表
            SumLabel.Text = "0.00";//初始化总金额数
            this.user = user;
            goods.Clear();
            Color userColor = MainForm.userColor;
            UIStyles.InitColorful(userColor, Color.White);
        }

        private void MyOrder_Click(object? sender, EventArgs e)
        {
            MyOrder myOrder = new MyOrder(user);
            myOrder.ShowDialog();
        }

        private void MyOwnSupermarket_Click(object sender, EventArgs e)
        {
            MySupermarket mySupermarket = new MySupermarket(user);
            mySupermarket.ShowDialog();
        }


        private void Function_Click(object sender, EventArgs e)
        {
            // 获取按钮的位置
            Point buttonLocation = Function.PointToScreen(Point.Empty);

            // 计算菜单的显示位置
            int x = buttonLocation.X;
            int y = buttonLocation.Y - Button1MenuStrip.Height;

            // 在按钮上方显示菜单
            Button1MenuStrip.Show(x, y);
        }
        public void ListItems(List<Item> items)//列表化商品
        {
            panel.Controls.Clear();
            panel.AutoScroll = true; // 启用自动滚动条
            int boxHeight = 160;
            int i = 0;
            foreach (Item item in items)//对于每个发送者有一个消息框
            {
                Itembox box = new Itembox(item, SumLabel, goods);
                box.Top = i * boxHeight; // 设置用户控件的位置
                i++;
                panel.Controls.Add(box); // 将用户控件添加到 Panel 中
            }
        }

        private void Search_Click(object sender, EventArgs e)
        {
            List<Item> items = new List<Item>();
            if (KeyTextBox.Text == "" || KeyTextBox.Text == null)
            {
                UIMessageBox.Show("请输入搜索关键字");
                //items = itemService.GetAllItem();
                //ListItems(items);
            }
            else
            {
                string key = KeyTextBox.Text;
                items = itemService.GetItemByKey(key);
                ListItems(items);
            }

            KeyTextBox.Clear();
        }

        private void BuyBtn_Click(object sender, EventArgs e)
        {
            if (goods.Count == 0)
            {
                UIMessageBox.Show("请选择需要购买的商品");
            }
            else
            {
                //添加订单order
                string orderID = Guid.NewGuid().ToString();
                double sum = SumLabel.Text.ToDouble();
                Order order = new Order(orderID, sum, DateTime.Now, user.UserID);
                //进入确认界面
                PayConfirm payConfirm = new PayConfirm(user, order, goods, this);
                payConfirm.Show();
                /*//更新商品列表和总金额
                List<Item> items = itemService.GetItemExceptUser(user);
                ListItems(items);
                SumLabel.Text = "0.00";
                //清空goods表，以进行下一笔订单的操作
                goods.Clear();*/
            }
        }

        private void Introduction_Click(object sender, EventArgs e)
        {
            Introduce introduce = new Introduce();
            introduce.ShowDialog();
        }

        private void KeyTextBox_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
